<?php namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Http\Requests\VoteRequest;
use App\Vote;
use App\VotePost;
use Auth;
use Illuminate\Http\Request;
use File;
use Carbon\Carbon;

class VotesController extends Controller
{

  public function __construct()
  {
    $this->middleware('auth', ['except' => 'votePost']);
  }

  /**
   * Show the form for creating a new resource.
   *
   * @return Response
   */
  public function create()
  {
    if (Auth::user()->votes()->first()) {
      return redirect()->to('votes/1/edit');
    }
    return view('votes.create');
  }

  /**
   * Store a newly created resource in storage.
   *
   * @return Response
   */
  public function store(VoteRequest $request)
  {
    $vote = new Vote();
    $vote->fill($request->all());
    if ($request->hasFile('img')) {
      $imgName = date('Y-m-d') . '_' . uniqid() . '.jpg';
      $img = $request->file('img')->move(public_path() . '/uploads/', $imgName);
      $vote->img_src = 'uploads/' . $img->getFilename();
    }
    Auth::user()->votes()->save($vote);

    return redirect()->to('home');
  }

  /**
   * Show the form for editing the specified resource.
   *
   * @param  int $id
   * @return Response
   */
  public function edit($id)
  {
    $vote = Auth::user()->votes()->first();
    return view('votes.edit')->with(compact('vote'));
  }

  /**
   * Update the specified resource in storage.
   *
   * @param  int $id
   * @param  VoteRequest $request
   * @return Response
   */
  public function update($id, VoteRequest $request)
  {
    $vote = Auth::user()->votes()->findorFail($id);
    $vote->fill($request->all());

    if ($request->hasFile('img')) {
      $imgName = date('Y-m-d') . '_' . uniqid() . '.jpg';
      $img = $request->file('img')->move(public_path() . '/uploads/', $imgName);
      File::delete(public_path(). '/'. $vote->img_src);
      $vote->img_src = 'uploads/' . $img->getFilename();
    }
    $vote->save();

    return redirect()->to('home');
  }

  /**
   * 投一票
   * @param int $id
   * @param Request $request
   * @return Response
   */
  public function votePost($id, Request $request)
  {
    $vote = Vote::findorFail($id);
    if ($vote) {
      if(Auth::check()) {
        $user_id = Auth::user()->id;
        $user_post = VotePost::where('user_id', $user_id)->orderBy('id','desc')->first();
        if ($user_post) {
          $last_time = Carbon::create()->diffInMinutes($user_post->updated_at);
          if($last_time<60) {
            return redirect()->back()->withErrors(['vote' => "一个小时之内只能投一票"]);
          }
        }
      } else {
        $user_id = 0;
        $post_ip = $request->ip();
        $ip_post = VotePost::where('ip', $post_ip)->orderBy('id','desc')->first();
        if ($ip_post) {
          $last_time = Carbon::create()->diffInMinutes($ip_post->updated_at);
          if($last_time<60) {
            return redirect()->back()->withErrors(['vote' => "一个小时之内只能投一票"]);
          }
        }
      }

      // 记录投票ip等信息
      $post = new VotePost();
      $post->ip = $request->ip();
      $post->user_id = $user_id;
      $result = $vote->votePosts()->save($post);
      if ($result) {
        $vote->increment('vote_count');
      }
    }

    return redirect()->back()->withErrors(['vote' => "投票成功，感谢您的参与！"]);
  }
}
